<!DOCTYPE HTML>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../../resources/gesture-util.js"></script>
<script src="../../../virtual/percent-based-scrolling/resources/percent-based-util.js"></script>
<style>
#targetDiv {
  width: 200px;
  height: 200px;
  overflow: scroll;
}

#innerDiv {
  width: 400px;
  height: 400px;
}
</style>

<body style="margin:0">
<div id="targetDiv">
  <div id="innerDiv">
  </div>
</div>
</body>

<script>
if (window.internals)
  internals.runtimeFlags.overscrollCustomizationEnabled = true;

var target_div = document.getElementById('targetDiv');
var rect = target_div.getBoundingClientRect();
var x = rect.left + rect.width / 2;
var y = rect.top + rect.height / 2;

// SPEED_INSTANT doesn't work here because tests are timing out.
// This could actually be a bug in how the overscroll event is computed.
// Possible explanation on what happens is that "leftover" delta from a single
// event doesn't cause an overscroll event, only when the next event arrives
// and the full delta is overscroll do we dispatch the event.
// When the speed is INSTANT we'll put the entire scrolling delta into a single
// event so we don't generate an overscroll event in that case.
const pixelsPerSec = 2000;
const pixelsToScroll = 600;
const { y: pixelsToScrollY } = calculatePixelsToScroll(
  target_div, pixelsToScroll, pixelsToScroll
);

var window_received_overscroll = false;

function onOverscroll(event) {
  assert_false(event.cancelable);
  // overscroll events targetting document are bubbled to the window.
  assert_true(event.bubbles);
  window_received_overscroll = true;
}
window.addEventListener("overscroll", onOverscroll);

async function test_overscroll_event_fire(source_device,
    precise_scrolling_delta) {
  window_received_overscroll = false;
  await waitForCompositorCommit();

  // Scroll up on target div and wait for the window to get overscroll event.
  await smoothScroll(
    pixelsToScrollY, x, y, source_device, "up", pixelsPerSec,
    precise_scrolling_delta
  );
  await waitFor(() => { return window_received_overscroll; },
      'Window did not receive overscroll event after scroll up on target.');
}

promise_test (async (t) => {
  // Make sure that no overscroll event is sent to target_div.
  target_div.addEventListener("overscroll",
      t.unreached_func("target_div got unexpected overscroll event."));
  return test_overscroll_event_fire(GestureSourceType.TOUCH_INPUT);
}, 'Tests that the window gets overscroll event when no element scrolls' +
   'after touch scrolling.');

promise_test (async (t) => {
  // Make sure that no overscroll event is sent to target_div.
  target_div.addEventListener("overscroll",
      t.unreached_func("target_div got unexpected overscroll event."));
  return test_overscroll_event_fire(GestureSourceType.MOUSE_INPUT, true);
}, 'Tests that the window gets overscroll event when no element scrolls ' +
   'after non-animated wheel scrolling.');

promise_test (async (t) => {
  // Make sure that no overscroll event is sent to target_div.
  target_div.addEventListener("overscroll",
      t.unreached_func("target_div got unexpected overscroll event."));
  return test_overscroll_event_fire(GestureSourceType.MOUSE_INPUT, false);
}, 'Tests that the window gets overscroll event when no element scrolls ' +
   'after animated wheel scrolling(smooth scrolling).');
</script>
